<?php
/**
* classe "royaume" pour authentification http
*
*/
class httpRealm {

	/**
	* @var string realm name/id
	*/
	var $name='httpRealm';
	
	/**
	*
	*/
	// var $type='Basic';
	
	/**
	* @var array login=>pwd
	*/
	var $users=array();

	/**
	* @var string provided user name
	*/
	var $user;
	
	/**
	* @var string provided pwd
	*/
	var $pwd;
	
	/**
	* constructor
	*/
	function httpRealm($name=null,$users=null) {
		$this->__construct($name,$users);
	}
	function __construct($name=null,$users=null) {
		if (is_string($name))
			$this->name=$name;
		if (is_array($users))
			$this->users=$users;
	}
	
	/**
	* require http auth if necessary
	* @access public
	* @return bool true if user/pwd provided
	*/
	function requireAuth() {
		if (!isset($_SERVER['PHP_AUTH_USER'])
		or !$this->_checkUser($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW'])) {
			// logged in
			header('WWW-Authenticate: Basic realm="'.$this->name.'"');
			header('HTTP/1.0 401 Unauthorized');
			return false;
		} else {
			// no valid user
			$this->login=$_SERVER['PHP_AUTH_USER'];
			$this->pwd=$_SERVER['PHP_AUTH_PW'];
			return true;
		}
	}/**/
	
	function getLogin() {
		return $this->login;
	}
	
	function getPwd() {
		return $this->pwd;
	}
	
	/**
	* checking login/pwd
	* @access protected
	*/
	function _checkUser($login,$pwd) {
		if (!isset($this->users[$login])
		or $this->users[$login]!=$pwd)
			return false;
		// valid user
		return true;
	}/**/
}
?>
